<?php

/**
 *      [品牌空间] (C)2001-2010 Comsenz Inc.
 *      This is NOT a freeware, use is subject to license terms
 *
 *      $Id: block.func.php 5720 2011-06-29 07:11:58Z menglingmin $
 */

if(!defined('IN_BRAND')) {
	exit('Access Denied');
}

function block_good($paramarr, $multicachekey='', $returncount=0){
	global $_G, $_SGLOBAL, $_BCACHE, $_SBLOCK, $_GET, $_SERVER;
    $catid = empty($paramarr['catid']) ? 0 : $paramarr['catid'];
    $sortby = $paramarr['order'];
    
    include_once('./batch.attribute.php');
    //店铺筛选
    
    //店铺分类筛选
    $shop_categorylist = getcategory('shop');
    $shop_searchcats = getsearchcats($shop_categorylist, $paramarr['shopcatid']);
    //店铺分类ID搜索条件
    if(!empty($paramarr['shopcatid'])) {
        $shoptagid[] = $paramarr['shopcatid'];
        if(is_array($shop_searchcats)) {
            foreach($shop_searchcats as $key=>$value) {
                $shoptagid[] = $value['catid'];
            }
        }
        $shoptagids = implode(",", $shoptagid);
        $shop_where[] = 'catid IN (' . $shoptagids . ')';
    } else {
        //$shop_where[] = 'catid > 0';
    }
    //店铺地区筛选
    $shop_regionlist = getcategory('region');
    $shop_searchregion = getsearchcats($shop_regionlist, $paramarr['region']);
    //店铺地区ID搜索条件
    if(!empty($paramarr['region'])) {
        $shopregionid[] = $paramarr['region'];
        if(is_array($shop_searchcats)) {
            foreach($shop_searchregion as $key=>$value) {
                $shopregionid[] = $value['catid'];
            }
        }
        $shopregionids = implode(",", $shopregionid);
        $shop_where[] = 'region IN (' . $shopregionids . ')';
    } else {
        //$shop_where[] = 'region > 0';
    }
    if(!empty($shop_where)) {
        $shopwheresql = implode(' AND ', $shop_where);
        //店铺筛选结果，
        $query = DB::query("SELECT itemid FROM ".DB::table("shopitems")." WHERE $shopwheresql");
        while($result = DB::fetch($query)) {
            $shopid_in[] = $result['itemid'];
        }
        if(!empty($shopid_in)) {
            $where[] = 'i.shopid IN ('.implode(',', $shopid_in).') ';
        } else {
            $where[] = 'i.shopid < 0';
        }
    }        
    
    
    //商品分类筛选
    $categorylist = getcategory('good'); //读商品分类
    
    $searchcats = getsearchcats($categorylist, $catid);
    
    // 分类ID搜索条件
    if(!empty($catid)) {
        $tagid[] = $catid;
        if(is_array($searchcats)) {
            foreach($searchcats as $key=>$value) {
                $tagid[] = $value['catid'];
            }
        }
        $tagids = implode(",", $tagid);
        $where[] = 'i.catid IN (' . $tagids . ')';
    } else {
        $where[] = 'i.catid > 0';
    }
    /*
    // 属性筛选器
    $attrvalues = empty($_GET['params'])?array():getattrvalues($_GET['params']);
    if($catid && $categorylist[$catid]['havechild'] == 0) {
        $attarray = formatattarr($catid, $attrvalues, $keyword);
    }
    */
    foreach($paramarr as $key=>$value) {
    	if(preg_match_all("/attr_ids_(\d+)/i", $key, $match)) {

    	    $attrvalues[$match[1][0]] = $value;
    	}
    }

    // 属性搜索
    $attr_in = getattr_in($catid, $attrvalues);
    
    // 条件拼合
    $attr_in!==NULL && $where[] = $attr_in;
    $where[] = 'i.grade_s > 2 AND i.grade > 2';
    //$keyword && $where[] = 'i.subject LIKE \'%'.$keyword.'%\'';
    if($catid>0 && $tagids) {
        $where[] = 'i.catid IN ('.$tagids.')';
    } else {
        $where[] = 'i.catid>0';
    }
    if($sortby == "viewnum") {
        $sql_orderby = ' ORDER BY i.viewnum '.$paramarr['sc'];
    } elseif($sortby == "lastpost") {
        $sql_orderby = ' ORDER BY i.lastpost '.$paramarr['sc'];
    } elseif($sortby == "replynum") {
        $sql_orderby = ' ORDER BY i.replynum '.$paramarr['sc'];
    } else {
        $sql_orderby = " ORDER BY i.displayorder ASC, i.itemid ".$paramarr['sc'];
    }
    
    $wheresql = implode(' AND ', $where);
    $limit = explode(",", $paramarr['limit']);
    $start = $limit[0];
    $perpage = $limit[1];

    // 数据查询，拆分SQL，分片缓存
    $_BCACHE->cachesql('block_'.$paramarr['cachename'], 'SELECT i.itemid, i.shopid FROM '.DB::table('gooditems').' i WHERE '.$wheresql . $sql_orderby, 0, 1, $perpage, $start, 'sitelist', 'good');
    foreach($_SBLOCK['block_'.$paramarr['cachename']] as $value) {
        $value = $_BCACHE->getiteminfo('good', $value['itemid'], $value['shopid']);
        $value['shopinfo'] = $_BCACHE->getshopinfo($value['shopid']);
        $value['intro'] = cutstr($value['intro'], 22, true);
        if ($viewby == "list") {
            $value['summary'] = cutstr($value['subject'], 39, true);
        } else {
            $value['summary'] = cutstr($value['subject'], 28, true);
        }
        if($viewby == 'list') {
            $value['thumb'] = str_replace('static/image/nophoto.gif', 'static/image/nophotosmall.gif', $value['thumb']);
        }
        $goodlist[] = $value;
    }
    return $goodlist;
}

function block_consume($paramarr, $multicachekey='', $returncount=0){
	global $_G, $_SGLOBAL, $_BCACHE, $_SBLOCK, $_GET, $_SERVER;
    $catid = empty($paramarr['catid']) ? 0 : $paramarr['catid'];
    $sortby = $paramarr['order'];
    
    include_once('./batch.attribute.php');
    //店铺筛选
    
    //店铺分类筛选
    $shop_categorylist = getcategory('shop');
    $shop_searchcats = getsearchcats($shop_categorylist, $paramarr['shopcatid']);
    //店铺分类ID搜索条件
    if(!empty($paramarr['shopcatid'])) {
        $shoptagid[] = $paramarr['shopcatid'];
        if(is_array($shop_searchcats)) {
            foreach($shop_searchcats as $key=>$value) {
                $shoptagid[] = $value['catid'];
            }
        }
        $shoptagids = implode(",", $shoptagid);
        $shop_where[] = 'catid IN (' . $shoptagids . ')';
    } else {
        //$shop_where[] = 'catid > 0';
    }
    //店铺地区筛选
    $shop_regionlist = getcategory('region');
    $shop_searchregion = getsearchcats($shop_regionlist, $paramarr['region']);
    //店铺地区ID搜索条件
    if(!empty($paramarr['region'])) {
        $shopregionid[] = $paramarr['region'];
        if(is_array($shop_searchcats)) {
            foreach($shop_searchregion as $key=>$value) {
                $shopregionid[] = $value['catid'];
            }
        }
        $shopregionids = implode(",", $shopregionid);
        $shop_where[] = 'region IN (' . $shopregionids . ')';
    } else {
        //$shop_where[] = 'region > 0';
    }
    if(!empty($shop_where)) {
        $shopwheresql = implode(' AND ', $shop_where);
        //店铺筛选结果，
        $query = DB::query("SELECT itemid FROM ".DB::table("shopitems")." WHERE $shopwheresql");
        while($result = DB::fetch($query)) {
            $shopid_in[] = $result['itemid'];
        }
        if(!empty($shopid_in)) {
            $where[] = 'i.shopid IN ('.implode(',', $shopid_in).') ';
        } else {
            $where[] = 'i.shopid < 0';
        }
    }        
    
    
    //消费券分类筛选
    $categorylist = getcategory('consume'); //读消费券分类
    
    $searchcats = getsearchcats($categorylist, $catid);
    
    // 分类ID搜索条件
    if(!empty($catid)) {
        $tagid[] = $catid;
        if(is_array($searchcats)) {
            foreach($searchcats as $key=>$value) {
                $tagid[] = $value['catid'];
            }
        }
        $tagids = implode(",", $tagid);
        $where[] = 'i.catid IN (' . $tagids . ')';
    } else {
        $where[] = 'i.catid > 0';
    }
    foreach($paramarr as $key=>$value) {
    	if(preg_match_all("/attr_ids_(\d+)/i", $key, $match)) {

    	    $attrvalues[$match[1][0]] = $value;
    	}
    }

    // 属性搜索
    $attr_in = getattr_in($catid, $attrvalues);
    
    // 条件拼合
    $attr_in!==NULL && $where[] = $attr_in;
    $where[] = 'i.grade_s > 2 AND i.grade > 2';
    //$keyword && $where[] = 'i.subject LIKE \'%'.$keyword.'%\'';
    if($catid>0 && $tagids) {
        $where[] = 'i.catid IN ('.$tagids.')';
    } else {
        $where[] = 'i.catid>0';
    }
    if(!empty($paramarr['validity_start'])) {
        $where[] = 'i.validity_start >= '.$paramarr['validity_start'];
    }
    if(!empty($paramarr['validity_end'])) {
        $where[] = 'i.validity_end <= '.$paramarr['validity_end'];
    }
    if($sortby == "viewnum") {
        $sql_orderby = ' ORDER BY i.viewnum '.$paramarr['sc'];
    } elseif($sortby == "lastpost") {
        $sql_orderby = ' ORDER BY i.lastpost '.$paramarr['sc'];
    } elseif($sortby == "replynum") {
        $sql_orderby = ' ORDER BY i.replynum '.$paramarr['sc'];
    } else {
        $sql_orderby = " ORDER BY i.displayorder ASC, i.itemid ".$paramarr['sc'];
    }
    
    $wheresql = implode(' AND ', $where);
    $limit = explode(",", $paramarr['limit']);
    $start = $limit[0];
    $perpage = $limit[1];

    // 数据查询，拆分SQL，分片缓存
    $_BCACHE->cachesql('block_'.$paramarr['cachename'], 'SELECT i.itemid, i.shopid FROM '.DB::table('consumeitems').' i WHERE '.$wheresql . $sql_orderby, 0, 1, $perpage, $start, 'sitelist', 'consume');
    foreach($_SBLOCK['block_'.$paramarr['cachename']] as $value) {
        $value = $_BCACHE->getiteminfo('consume', $value['itemid'], $value['shopid']);
        $value['shopinfo'] = $_BCACHE->getshopinfo($value['shopid']);
        $value['intro'] = cutstr($value['intro'], 22, true);
        if ($viewby == "list") {
            $value['summary'] = cutstr($value['subject'], 39, true);
        } else {
            $value['summary'] = cutstr($value['subject'], 28, true);
        }
        if($viewby == 'list') {
            $value['thumb'] = str_replace('static/image/nophoto.gif', 'static/image/nophotosmall.gif', $value['thumb']);
        }
        $consumelist[] = $value;
    }
    return $consumelist;
}

function block_notice($paramarr, $multicachekey='', $returncount=0){
	global $_G, $_SGLOBAL, $_BCACHE, $_SBLOCK, $_GET, $_SERVER;
    $catid = empty($paramarr['catid']) ? 0 : $paramarr['catid'];
    $sortby = $paramarr['order'];
    
    include_once('./batch.attribute.php');
    //店铺筛选
    
    //店铺分类筛选
    $shop_categorylist = getcategory('shop');
    $shop_searchcats = getsearchcats($shop_categorylist, $paramarr['shopcatid']);
    //店铺分类ID搜索条件
    if(!empty($paramarr['shopcatid'])) {
        $shoptagid[] = $paramarr['shopcatid'];
        if(is_array($shop_searchcats)) {
            foreach($shop_searchcats as $key=>$value) {
                $shoptagid[] = $value['catid'];
            }
        }
        $shoptagids = implode(",", $shoptagid);
        $shop_where[] = 'catid IN (' . $shoptagids . ')';
    } else {
        //$shop_where[] = 'catid > 0';
    }
    //店铺地区筛选
    $shop_regionlist = getcategory('region');
    $shop_searchregion = getsearchcats($shop_regionlist, $paramarr['region']);
    //店铺地区ID搜索条件
    if(!empty($paramarr['region'])) {
        $shopregionid[] = $paramarr['region'];
        if(is_array($shop_searchcats)) {
            foreach($shop_searchregion as $key=>$value) {
                $shopregionid[] = $value['catid'];
            }
        }
        $shopregionids = implode(",", $shopregionid);
        $shop_where[] = 'region IN (' . $shopregionids . ')';
    } else {
        //$shop_where[] = 'region > 0';
    }
    if(!empty($shop_where)) {
        $shopwheresql = implode(' AND ', $shop_where);
        //店铺筛选结果，
        $query = DB::query("SELECT itemid FROM ".DB::table("shopitems")." WHERE $shopwheresql");
        while($result = DB::fetch($query)) {
            $shopid_in[] = $result['itemid'];
        }
        if(!empty($shopid_in)) {
            $where[] = 'i.shopid IN ('.implode(',', $shopid_in).') ';
        } else {
            $where[] = 'i.shopid < 0';
        }
    }        
    
    
    //公告分类筛选
    $categorylist = getcategory('notice'); //读公告分类
    
    $searchcats = getsearchcats($categorylist, $catid);
    
    // 分类ID搜索条件
    if(!empty($catid)) {
        $tagid[] = $catid;
        if(is_array($searchcats)) {
            foreach($searchcats as $key=>$value) {
                $tagid[] = $value['catid'];
            }
        }
        $tagids = implode(",", $tagid);
        $where[] = 'i.catid IN (' . $tagids . ')';
    } else {
        $where[] = 'i.catid > 0';
    }
    foreach($paramarr as $key=>$value) {
    	if(preg_match_all("/attr_ids_(\d+)/i", $key, $match)) {

    	    $attrvalues[$match[1][0]] = $value;
    	}
    }

    // 属性搜索
    $attr_in = getattr_in($catid, $attrvalues);
    
    // 条件拼合
    $attr_in!==NULL && $where[] = $attr_in;
    $where[] = 'i.grade_s > 2 AND i.grade > 2';
    //$keyword && $where[] = 'i.subject LIKE \'%'.$keyword.'%\'';
    if($catid>0 && $tagids) {
        $where[] = 'i.catid IN ('.$tagids.')';
    } else {
        $where[] = 'i.catid>0';
    }
    if($sortby == "viewnum") {
        $sql_orderby = ' ORDER BY i.viewnum '.$paramarr['sc'];
    } elseif($sortby == "lastpost") {
        $sql_orderby = ' ORDER BY i.lastpost '.$paramarr['sc'];
    } elseif($sortby == "replynum") {
        $sql_orderby = ' ORDER BY i.replynum '.$paramarr['sc'];
    } else {
        $sql_orderby = " ORDER BY i.displayorder ASC, i.itemid ".$paramarr['sc'];
    }
    
    $wheresql = implode(' AND ', $where);
    $limit = explode(",", $paramarr['limit']);
    $start = $limit[0];
    $perpage = $limit[1];

    // 数据查询，拆分SQL，分片缓存
    $_BCACHE->cachesql('block_'.$paramarr['cachename'], 'SELECT i.itemid, i.shopid FROM '.DB::table('noticeitems').' i WHERE '.$wheresql . $sql_orderby, 0, 1, $perpage, $start, 'sitelist', 'notice');
    foreach($_SBLOCK['block_'.$paramarr['cachename']] as $value) {
        $value = $_BCACHE->getiteminfo('notice', $value['itemid'], $value['shopid']);
        $value['shopinfo'] = $_BCACHE->getshopinfo($value['shopid']);
        $value['intro'] = cutstr($value['intro'], 22, true);
        if ($viewby == "list") {
            $value['summary'] = cutstr($value['subject'], 39, true);
        } else {
            $value['summary'] = cutstr($value['subject'], 28, true);
        }
        if($viewby == 'list') {
            $value['thumb'] = str_replace('static/image/nophoto.gif', 'static/image/nophotosmall.gif', $value['thumb']);
        }
        $noticelist[] = $value;
    }
    return $noticelist;
}

function block_album($paramarr, $multicachekey='', $returncount=0){
	global $_G, $_SGLOBAL, $_BCACHE, $_SBLOCK, $_GET, $_SERVER;
    $catid = empty($paramarr['catid']) ? 0 : $paramarr['catid'];
    $sortby = $paramarr['order'];
    
    include_once('./batch.attribute.php');
    //店铺筛选
    
    //店铺分类筛选
    $shop_categorylist = getcategory('shop');
    $shop_searchcats = getsearchcats($shop_categorylist, $paramarr['shopcatid']);
    //店铺分类ID搜索条件
    if(!empty($paramarr['shopcatid'])) {
        $shoptagid[] = $paramarr['shopcatid'];
        if(is_array($shop_searchcats)) {
            foreach($shop_searchcats as $key=>$value) {
                $shoptagid[] = $value['catid'];
            }
        }
        $shoptagids = implode(",", $shoptagid);
        $shop_where[] = 'catid IN (' . $shoptagids . ')';
    } else {
        //$shop_where[] = 'catid > 0';
    }
    //店铺地区筛选
    $shop_regionlist = getcategory('region');
    $shop_searchregion = getsearchcats($shop_regionlist, $paramarr['region']);
    //店铺地区ID搜索条件
    if(!empty($paramarr['region'])) {
        $shopregionid[] = $paramarr['region'];
        if(is_array($shop_searchcats)) {
            foreach($shop_searchregion as $key=>$value) {
                $shopregionid[] = $value['catid'];
            }
        }
        $shopregionids = implode(",", $shopregionid);
        $shop_where[] = 'region IN (' . $shopregionids . ')';
    } else {
        //$shop_where[] = 'region > 0';
    }
    if(!empty($shop_where)) {
        $shopwheresql = implode(' AND ', $shop_where);
        //店铺筛选结果，
        $query = DB::query("SELECT itemid FROM ".DB::table("shopitems")." WHERE $shopwheresql");
        while($result = DB::fetch($query)) {
            $shopid_in[] = $result['itemid'];
        }
        if(!empty($shopid_in)) {
            $where[] = 'i.shopid IN ('.implode(',', $shopid_in).') ';
        } else {
            $where[] = 'i.shopid < 0';
        }
    }        
    
    
    //公告分类筛选
    $categorylist = getcategory('notice'); //读公告分类
    
    $searchcats = getsearchcats($categorylist, $catid);
    
    // 分类ID搜索条件
    if(!empty($catid)) {
        $tagid[] = $catid;
        if(is_array($searchcats)) {
            foreach($searchcats as $key=>$value) {
                $tagid[] = $value['catid'];
            }
        }
        $tagids = implode(",", $tagid);
        $where[] = 'i.catid IN (' . $tagids . ')';
    } else {
        $where[] = 'i.catid > 0';
    }
    foreach($paramarr as $key=>$value) {
    	if(preg_match_all("/attr_ids_(\d+)/i", $key, $match)) {

    	    $attrvalues[$match[1][0]] = $value;
    	}
    }

    // 属性搜索
    $attr_in = getattr_in($catid, $attrvalues);
    
    // 条件拼合
    $attr_in!==NULL && $where[] = $attr_in;
    $where[] = 'i.grade_s > 2 AND i.grade > 2';
    //$keyword && $where[] = 'i.subject LIKE \'%'.$keyword.'%\'';
    if($catid>0 && $tagids) {
        $where[] = 'i.catid IN ('.$tagids.')';
    } else {
        $where[] = 'i.catid>0';
    }
    if($sortby == "viewnum") {
        $sql_orderby = ' ORDER BY i.viewnum '.$paramarr['sc'];
    } elseif($sortby == "lastpost") {
        $sql_orderby = ' ORDER BY i.lastpost '.$paramarr['sc'];
    } elseif($sortby == "replynum") {
        $sql_orderby = ' ORDER BY i.replynum '.$paramarr['sc'];
    } else {
        $sql_orderby = " ORDER BY i.displayorder ASC, i.itemid ".$paramarr['sc'];
    }
    
    $wheresql = implode(' AND ', $where);
    $limit = explode(",", $paramarr['limit']);
    $start = $limit[0];
    $perpage = $limit[1];

    // 数据查询，拆分SQL，分片缓存
    $_BCACHE->cachesql('block_'.$paramarr['cachename'], 'SELECT i.itemid, i.shopid FROM '.DB::table('albumitems').' i WHERE '.$wheresql . $sql_orderby, 0, 1, $perpage, $start, 'sitelist', 'notice');
    foreach($_SBLOCK['block_'.$paramarr['cachename']] as $value) {
        $value = $_BCACHE->getiteminfo('album', $value['itemid'], $value['shopid']);
        $value['shopinfo'] = $_BCACHE->getshopinfo($value['shopid']);
        $value['intro'] = cutstr($value['intro'], 22, true);
        if ($viewby == "list") {
            $value['summary'] = cutstr($value['subject'], 39, true);
        } else {
            $value['summary'] = cutstr($value['subject'], 28, true);
        }
        if($viewby == 'list') {
            $value['thumb'] = str_replace('static/image/nophoto.gif', 'static/image/nophotosmall.gif', $value['thumb']);
        }
        $noticelist[] = $value;
    }
    return $noticelist;
}

function block_groupbuy($paramarr, $multicachekey='', $returncount=0){
	global $_G, $_SGLOBAL, $_BCACHE, $_SBLOCK, $_GET, $_SERVER;
    $catid = empty($paramarr['catid']) ? 0 : $paramarr['catid'];
    $sortby = $paramarr['order'];
    
    include_once('./batch.attribute.php');
    //店铺筛选
    
    //店铺分类筛选
    $shop_categorylist = getcategory('shop');
    $shop_searchcats = getsearchcats($shop_categorylist, $paramarr['shopcatid']);
    //店铺分类ID搜索条件
    if(!empty($paramarr['shopcatid'])) {
        $shoptagid[] = $paramarr['shopcatid'];
        if(is_array($shop_searchcats)) {
            foreach($shop_searchcats as $key=>$value) {
                $shoptagid[] = $value['catid'];
            }
        }
        $shoptagids = implode(",", $shoptagid);
        $shop_where[] = 'catid IN (' . $shoptagids . ')';
    } else {
        //$shop_where[] = 'catid > 0';
    }
    //店铺地区筛选
    $shop_regionlist = getcategory('region');
    $shop_searchregion = getsearchcats($shop_regionlist, $paramarr['region']);
    //店铺地区ID搜索条件
    if(!empty($paramarr['region'])) {
        $shopregionid[] = $paramarr['region'];
        if(is_array($shop_searchcats)) {
            foreach($shop_searchregion as $key=>$value) {
                $shopregionid[] = $value['catid'];
            }
        }
        $shopregionids = implode(",", $shopregionid);
        $shop_where[] = 'region IN (' . $shopregionids . ')';
    } else {
        //$shop_where[] = 'region > 0';
    }
    if(!empty($shop_where)) {
        $shopwheresql = implode(' AND ', $shop_where);
        //店铺筛选结果，
        $query = DB::query("SELECT itemid FROM ".DB::table("shopitems")." WHERE $shopwheresql");
        while($result = DB::fetch($query)) {
            $shopid_in[] = $result['itemid'];
        }
        if(!empty($shopid_in)) {
            $where[] = 'i.shopid IN ('.implode(',', $shopid_in).') ';
        } else {
            $where[] = 'i.shopid < 0';
        }
    }        
    
    
    //团购分类筛选
    $categorylist = getcategory('groupbuy'); //读团购分类
    
    $searchcats = getsearchcats($categorylist, $catid);
    
    // 分类ID搜索条件
    if(!empty($catid)) {
        $tagid[] = $catid;
        if(is_array($searchcats)) {
            foreach($searchcats as $key=>$value) {
                $tagid[] = $value['catid'];
            }
        }
        $tagids = implode(",", $tagid);
        $where[] = 'i.catid IN (' . $tagids . ')';
    } else {
        $where[] = 'i.catid > 0';
    }
    if(!empty($paramarr['validity_start'])) {
        $where[] = 'i.validity_start >= '.$paramarr['validity_start'];
    }
    if(!empty($paramarr['validity_end'])) {
        $where[] = 'i.validity_end <= '.$paramarr['validity_end'];
    }
    foreach($paramarr as $key=>$value) {
    	if(preg_match_all("/attr_ids_(\d+)/i", $key, $match)) {

    	    $attrvalues[$match[1][0]] = $value;
    	}
    }

    // 属性搜索
    $attr_in = getattr_in($catid, $attrvalues);
    
    // 条件拼合
    $attr_in!==NULL && $where[] = $attr_in;
    $where[] = 'i.grade_s > 2 AND i.grade > 2';
    //$keyword && $where[] = 'i.subject LIKE \'%'.$keyword.'%\'';
    if($catid>0 && $tagids) {
        $where[] = 'i.catid IN ('.$tagids.')';
    } else {
        $where[] = 'i.catid>0';
    }
    if($sortby == "viewnum") {
        $sql_orderby = ' ORDER BY i.viewnum '.$paramarr['sc'];
    } elseif($sortby == "lastpost") {
        $sql_orderby = ' ORDER BY i.lastpost '.$paramarr['sc'];
    } elseif($sortby == "replynum") {
        $sql_orderby = ' ORDER BY i.replynum '.$paramarr['sc'];
    } else {
        $sql_orderby = " ORDER BY i.displayorder ASC, i.itemid ".$paramarr['sc'];
    }
    
    $wheresql = implode(' AND ', $where);
    $limit = explode(",", $paramarr['limit']);
    $start = $limit[0];
    $perpage = $limit[1];

    // 数据查询，拆分SQL，分片缓存
    $_BCACHE->cachesql('block_'.$paramarr['cachename'], 'SELECT i.itemid, i.shopid FROM '.DB::table('groupbuyitems').' i WHERE '.$wheresql . $sql_orderby, 0, 1, $perpage, $start, 'sitelist', 'groupbuy');
    foreach($_SBLOCK['block_'.$paramarr['cachename']] as $value) {
        $value = $_BCACHE->getiteminfo('groupbuy', $value['itemid'], $value['shopid']);
        $value['shopinfo'] = $_BCACHE->getshopinfo($value['shopid']);
        $value['intro'] = cutstr($value['intro'], 22, true);
        if ($viewby == "list") {
            $value['summary'] = cutstr($value['subject'], 39, true);
        } else {
            $value['summary'] = cutstr($value['subject'], 28, true);
        }
        if($viewby == 'list') {
            $value['thumb'] = str_replace('static/image/nophoto.gif', 'static/image/nophotosmall.gif', $value['thumb']);
        }
        $groupbuylist[] = $value;
    }
    return $groupbuylist;
}

function block_sql($paramarr, $multicachekey='', $returncount=0) {
	global $_G, $_SGLOBAL, $_GET, $_SERVER;

	if(!empty($paramarr['sql'])) {
		require_once(B_ROOT.'./source/function/block_sql.func.php');
		if($returncount>0) {
			return runsql($paramarr, '', 1);
		}
		list($sqlstring, $listcount) = runsql($paramarr, '', 0, $multicachekey);
		if(!empty($paramarr['perpage'])) {
			if($listcount) {
				$urlarr = $_GET;
				foreach($urlarr as $key=>$value) {
					if(empty($value)) {
						unset($urlarr[$key]);
					} else {
						$urlarr[$key] = rawurlencode(stripslashes($urlarr[$key]));
					}
				}
				unset($urlarr['page']);
				$phpurl = arraytostring($urlarr, '=', '&');
				$phpurl = $phpurl?('http://'.$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'].'?'.$phpurl):('http://'.$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME']);
				$theblockarr['multipage'] = multi($listcount, $paramarr['perpage'], $_GET['page'], rawurldecode($phpurl), 1);
				$theblockarr['listcount'] = $listcount;
			}
		}
	}

	//查询数据
	if($listcount) {
		//查询
		$query = DB::query($sqlstring);
		while ($value = DB::fetch($query)) {
			if(isset($value['subjectimage'])) {
				if(!empty($paramarr['extra'])) {
					$thumbwidth = $paramarr['extra']['thumbwidth'];
					$thumbheight = $paramarr['extra']['thumbheight'];
					$value["thumb-{$thumbwidth}x{$thumbheight}"] = getattachurl(mktempthumb($value['subjectimage'], $thumbwidth, $thumbheight));
				}
				$value['thumb'] = getattachurl($value['subjectimage'], 1);
				$value['subjectimage'] = getattachurl($value['subjectimage']);
			}
			$theblockarr[] = $value;
		}
	} 
	return $theblockarr;
}

function stripbbcode($string) {
	return preg_replace("/\[.+?\]/i", '', $string);
}

?>